package com.cfun.mylearn.Support.utils;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import com.cfun.mylearn.Support.base.BaseApplication;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

public class CommonUtils {


	public static String VERSION_CODE = "";
	public static String VERSION_NAME = "";
	public static String MARKET_NAME = "";
	
	public static String getCustomAgent() {
		String marketName = getMarketName();
		String device_uuid = ResourceReader.getUuid();
		return " (zjxw;" + getVersionName() + ";" + Build.MODEL + ";"
				+ Build.VERSION.RELEASE + ";"
				+ Locale.getDefault().getLanguage() + "/"
				+ Locale.getDefault().getCountry() + ";" + marketName + ";"
				+ device_uuid + ")";
	}
	
	public static String getMarketName() {
		if (TextUtils.isEmpty(MARKET_NAME)) {
			MARKET_NAME = ResourceReader.readString(Spkeys.MARKET_NAME);
			if (TextUtils.isEmpty(MARKET_NAME)) {
				MARKET_NAME = String.valueOf(ResourceReader
						.readMetaString("UMENG_CHANNEL"));
				ResourceWrite.write2sp(Spkeys.MARKET_NAME, MARKET_NAME);
			}
		}
		return MARKET_NAME;
	}
	
	public static String getVersionCode() {
		if (TextUtils.isEmpty(VERSION_CODE) && BaseApplication.getInstance() != null) {
			VERSION_CODE = String.valueOf(ResourceReader.readAppVersionCode());
		}
		return VERSION_CODE;
	}
	
	public static String getVersionName() {
		if (StringUtils.isEmpty(VERSION_NAME) && BaseApplication.getInstance() != null) {
			VERSION_NAME = ResourceReader.readAppVersion(BaseApplication.getInstance());
		}
		return VERSION_NAME;
	}
	
	public static String getNotifyString(long count) {
		if (count > 99) {
			return "99+";
		} else {
			return String.valueOf(count);
		}
	}
	
	public static Map<String, String> getWebViewHeader() {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("X-Referer", "http://zjxwapp/");
		return map;
	}
	
	public static boolean isHONEYCOMB() {
		return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;
	}
	
	/**
	 * 默认生成8位的随机字符串
	 * @return
	 */
	public static String getRandomString() {
		int length = 8;
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern
				.compile("((1[3,5,8][0-9])|(14[5,7])|(17[0,6,7,8]))//d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	public static String getMD5Hex(String str) {
		byte[] data = getMD5(str.getBytes(Charset.forName("UTF-8")));
		BigInteger bi = new BigInteger(data).abs();
		String result = bi.toString(36);
		return result;
	}

	private static byte[] getMD5(byte[] data) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(data);
			return digest.digest();
		} catch (NoSuchAlgorithmException e) {
			ELog.printStackTrace("", e);
		}
		return null;
	}

	public static String getCommentCount4View(long commentCount) {
		String str = "";
		if (commentCount <= 9999) {
			str = commentCount + "";
		} else {
			long baseCount = 10000;
			String baseCountStr = "万";
			if(commentCount > 99999999) {
				baseCount = 100000000;
				baseCountStr = "亿";
			}
			DecimalFormat decimalFormat =new DecimalFormat(".0");//构造方法的字符格式这里如果小数不足2位,会以0补足.
			decimalFormat.setRoundingMode(RoundingMode.DOWN);
			String p= decimalFormat.format(((float)commentCount / baseCount));
			if(p.endsWith(".0")) {
				p = p.substring(0, p.indexOf("."));
			}
			if(0 == commentCount % (baseCount / 10)) {
				str = p + baseCountStr;
			} else {
				str = p + baseCountStr + "+";
			}
		}

		return str;
	}

	public static String getRCount4View(long rCount) {
		StringBuffer str = new StringBuffer("");
		if (rCount <= 9999) {
			str.append(rCount);
		} else {
			long baseCount = 10000;
			String unitStr = "万";
			if(rCount > 99999999) {
				baseCount = 100000000;
				unitStr = "亿";
			}
			DecimalFormat decimalFormat =new DecimalFormat("##0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
			decimalFormat.setRoundingMode(RoundingMode.DOWN);
			String p= decimalFormat.format(((float) rCount / baseCount));
			int pointIndex = p.indexOf(".");
			int integer = Integer.valueOf(p.substring(0, p.indexOf(".")));
			int decimal1 = Integer.valueOf(p.substring(pointIndex + 1, p.length() - 1));
			if (decimal1 == 0) {
				str.append(integer).append(unitStr);
			} else {
				str.append(integer).append(".").append(decimal1).append(unitStr);
			}
		}

		return str.toString();
	}

	public static boolean copyFile(InputStream sourceFile, File destFile) throws IOException {
		OutputStream out = new FileOutputStream(destFile);
		byte[] buf = new byte[4096];
		int len;
		while ((len = sourceFile.read(buf)) > 0) {
			Thread.yield();
			out.write(buf, 0, len);
		}
		out.close();
		return true;
	}

	public static boolean copyFile(File sourceFile, File destFile) throws IOException {
		if (!destFile.exists()) {
			destFile.createNewFile();
		}
		FileInputStream source = null;
		FileOutputStream destination = null;
		try {
			source = new FileInputStream(sourceFile);
			destination = new FileOutputStream(destFile);
			destination.getChannel().transferFrom(source.getChannel(), 0, source.getChannel().size());
		} catch (Exception e) {

			return false;
		} finally {
			if (source != null) {
				source.close();
			}
			if (destination != null) {
				destination.close();
			}
		}
		return true;
	}
}
